package com.zmn.oms.business.impl.messageV1.text;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.zmn.base.product.common.dto.product.ProductBaseQuery;
import com.zmn.base.product.common.dto.product.ec.ProductBaseDRO;
import com.zmn.base.product.dubbo.interfaces.product.foreign.ProductForeignListRemoteService;
import com.zmn.base.shortlink.common.enums.shortlink.ShortLinkTypeEnum;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.business.interfaces.messageV1.common.MessageGetSendInfoService;
import com.zmn.oms.business.interfaces.messageV1.text.CustomerMessageSendTextService;
import com.zmn.oms.business.interfaces.messageV1.text.MessageSendTextService;
import com.zmn.oms.common.constant.MessageConsts;
import com.zmn.oms.model.dto.messageV1.CustomerSendMessageDIO;
import com.zmn.oms.model.dto.messageV1.ShortUrlDTO;
import com.zmn.oms.model.dto.messageV1.TextMessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：
 *
 * @author liuyingying
 * @since 2022/9/22 20:02
 */
@Service
@Slf4j
public class CustomerMessageSendTextServiceImpl implements CustomerMessageSendTextService {

    @DubboReference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    protected ProductForeignListRemoteService productForeignListRemoteService;
    // 商品详情页：/subPackages/product/detail?id=产品id
    @Value("${oms.wechat.detail.quotation3.url:/subPackages/product/detail?id=%s}")
    private String productDetailWechatUrl;
    @Resource
    private MessageGetSendInfoService messageGetSendInfoService;
    @Resource
    private MessageSendTextService messageSendTextService;

    @Override
    public void sendOrderTransferFailMessage(CustomerSendMessageDIO customerSendMessageDIO) {
        Objects.requireNonNull(customerSendMessageDIO);
        Objects.requireNonNull(customerSendMessageDIO.getTelphone());
        Objects.requireNonNull(customerSendMessageDIO.getProductId());

        ShortUrlDTO shortUrlDTO = ShortUrlDTO.builder()
                .validEndTime(DateUtil.addDays(DateUtil.getNow(), 7))
                .orderId(Optional.ofNullable(customerSendMessageDIO.getOrderId()).orElse(0L))
                .ruleId(MessageConsts.ZMN_MSG_RULEID_ORDER_TRANSFER_FAIL)
                .miniProgramsLink(String.format(productDetailWechatUrl, customerSendMessageDIO.getProductId()))
                .shortLinkType(ShortLinkTypeEnum.MINIPROGRAMS_SHORTLINK.getKey())
                .build();


        Map<String, String> params = Maps.newHashMap();
        params.put("short_link", messageGetSendInfoService.getShortUrl(shortUrlDTO));
        params.put("product_name", this.getProductName(customerSendMessageDIO.getProductId()));

        TextMessageDTO textMessageDTO = TextMessageDTO.builder()
                .ruleId(MessageConsts.ZMN_MSG_RULEID_ORDER_TRANSFER_FAIL)
                .orderId(customerSendMessageDIO.getOrderId())
                .mobile(customerSendMessageDIO.getTelphone())
                .channelId(customerSendMessageDIO.getChannelId())
                .cityId(customerSendMessageDIO.getCityId())
                .params(JSON.toJSONString(params)).build();
        messageSendTextService.sendTextMessage(textMessageDTO);
    }

    @Override
    public void sendOrderConfirmQuotedFailMessage(CustomerSendMessageDIO customerSendMessageDIO) {
        Objects.requireNonNull(customerSendMessageDIO);
        Objects.requireNonNull(customerSendMessageDIO.getTelphone());
        Objects.requireNonNull(customerSendMessageDIO.getProductId());

        ShortUrlDTO shortUrlDTO = ShortUrlDTO.builder()
                .orderId(customerSendMessageDIO.getOrderId())
                .validEndTime(DateUtil.addDays(DateUtil.getNow(), 7))
                .ruleId(MessageConsts.ZMN_MSG_RULEID_ORDER_CONFIRM_QUOTED_FAIL)
                .miniProgramsLink(String.format(productDetailWechatUrl, customerSendMessageDIO.getProductId()))
                .shortLinkType(ShortLinkTypeEnum.MINIPROGRAMS_SHORTLINK.getKey())
                .build();

        Map<String, String> params = Maps.newHashMap();
        params.put("short_link", messageGetSendInfoService.getShortUrl(shortUrlDTO));
        params.put("product_name", this.getProductName(customerSendMessageDIO.getProductId()));
        params.put("contact_time", customerSendMessageDIO.getDutyTime() == null ? "" : DateUtil.toString(customerSendMessageDIO.getDutyTime(), DateUtil.FORMAT_DEFAULT));

        TextMessageDTO textMessageDTO = TextMessageDTO.builder()
                .ruleId(MessageConsts.ZMN_MSG_RULEID_ORDER_CONFIRM_QUOTED_FAIL)
                .orderId(customerSendMessageDIO.getOrderId())
                .mobile(customerSendMessageDIO.getTelphone())
                .channelId(customerSendMessageDIO.getChannelId())
                .cityId(customerSendMessageDIO.getCityId())
                .params(JSON.toJSONString(params)).build();
        messageSendTextService.sendTextMessage(textMessageDTO);
    }

    /**
     * 获取产品名称
     *
     * @param productId
     * @return
     */
    private String getProductName(Integer productId) {
        ProductBaseQuery productDIO = new ProductBaseQuery();
        productDIO.setProductId(productId);
        ResponseDTO<List<ProductBaseDRO>> responseDTO = productForeignListRemoteService.listBaseDROByQuery(productDIO);
        if (CollectionUtils.isEmpty(responseDTO.getData())) {
            return "";
        }
        return responseDTO.getData().get(0).getProductName();
    }
}
